/*
 * Copyright 2010-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jslet.paas.modelobject.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.eclipse.persistence.dynamic.DynamicEntity;
import org.jslet.paas.modelmeta.dao.ModelMetaDao;
import org.jslet.paas.modelmeta.model.ModelMeta;
import org.jslet.paas.modelmeta.model.ModelMetaConfiguration;
import org.jslet.paas.modelobject.dao.ModelObjectDao;
import org.jslet.paas.filter.model.FilterItem;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;

@Service(value="modelObjectService")
public class ModelObjectServiceImpl implements ModelObjectService
{
	@Resource(name = "modelObjectDao")
	private ModelObjectDao modelObjectDAO;

	@Resource(name = "modelMetaDao")
	private ModelMetaDao modelMetaDataDao;
	
	public List<ModelMeta> getMetaData()
	{
		return ModelMetaConfiguration.SINGLETON.getAllBoConfig();
//		return this.bizMetaDataDao.findAll();
	}

	@PreAuthorize("@servicePermissionJudge.judge(#modelName, \"find\")")
	public List<DynamicEntity> findAll(String modelName, int pageNum, int pageSize, List<HashMap<String, Object>> filters)
	{
		return this.modelObjectDAO.findAll(modelName, pageNum, pageSize, this.convertFilterItem(filters));
	}

	@PreAuthorize("@servicePermissionJudge.judge(#modelName, \"create\")")
	public List<HashMap<String, Object>> createModelObject(String modelName, List<HashMap<String, Object>> newData)
	{
		this.modelObjectDAO.createModelObject(modelName, newData);
		return newData;
	}

	@PreAuthorize("@servicePermissionJudge.judge(#modelName, \"update\")")
	public List<HashMap<String, Object>> updateModelObject(final String modelName,
		final List<HashMap<String, Object>> modifiedData)
	{
		modelObjectDAO.updateModelObject(modelName, modifiedData);
		return modifiedData;
	}

	@PreAuthorize("@servicePermissionJudge.judge(#modelName, \"delete\")")
	public List<HashMap<String, Object>> deleteModelObject(String modelName,
		List<HashMap<String, Object>> delData)
	{
		this.modelObjectDAO.deleteModelObject(modelName, delData);
		return delData;
	}

	private List<FilterItem> convertFilterItem(List<HashMap<String, Object>> filters){
		if(filters == null){
			return null;
		}
		List<FilterItem> result = new ArrayList<FilterItem>();
		FilterItem filterItem;
		for(HashMap<String, Object> item : filters){
			filterItem = new FilterItem(item.get("field").toString(),
				item.get("opr").toString(),
				item.get("value")
				);
			result.add(filterItem);
		}
		return result;
	}
}
